package com.system.boot.setting.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.api.BaseDefauted;
import com.system.api.setting.entity.dto.DictItemFormDto;
import com.system.api.setting.entity.po.SysDictItem;
import com.system.api.setting.entity.search.DictItemSearch;
import com.system.boot.setting.converter.DictItemConverter;
import com.system.boot.setting.service.DictItemService;
import java.util.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.yang.common.core.exception.BaseException;
import org.yang.common.core.result.BaseController;
import org.yang.common.core.result.R;
import org.yang.database.orm.entity.BaseEnabled;
import org.yang.database.orm.entity.BaseQuery;
import org.yang.database.orm.entity.Options;

/**
 * 字典词条
 *
 * @author: lslands
 * @since: 1.0
 * @create: 2024/5/18
 * @description:
 */
@Slf4j
@RestController
@RequestMapping("/dict-item")
@RequiredArgsConstructor
public class DictItemController extends BaseController {

  private final DictItemService dictItemService;
  private final DictItemConverter dictItemConverter;

  /**
   * 新增/修改
   *
   * @param dto 新增对象
   * @apiNote 通过是否存在主键,判定是新增还是修改;id为空则为新增，反之;
   * @author lslands
   * @date 2024/5/18 23:06
   * @return R<void>
   */
  @PostMapping("/saveOrUpdate")
  public R save(@RequestBody DictItemFormDto dto) throws BaseException {
    SysDictItem record = dictItemConverter.formEntity(dto);
    return Objects.isNull(dto.getId())
            ? toCreateAjax(dictItemService.handInsert(record))
            : toUpdateAjax(dictItemService.handUpdateById(record));
  }

  /**
   * 更新状态
   * @param record 更新状态对象
   * @return R<void>
   */
  @PatchMapping("/updateStatus")
  public R updateStatus(@RequestBody BaseEnabled record) throws BaseException {
    return toUpdateAjax(
        dictItemService.handEnabledById(record.getId(), record.getEnabled(), record.getRevision()));
  }

  /**
   * 字典列表
   * @param dictCode 字典编码
   * @return R<List<Options>>
   * @throws BaseException 异常
   */
  @GetMapping("/options")
  public R<List<Options>> options(@RequestParam String dictCode) throws BaseException {

    return R.success(dictItemService.getOptions(dictCode));
  }

  /**
   * 更新默认值
   * @param record 更新默认值对象
   * @return R<void>
   */
  @PatchMapping("/handDefauted")
  public R handDefauted(@RequestBody BaseDefauted record) throws BaseException {
    return toUpdateAjax(
            dictItemService.handDefautedById(record.getId(),
                    record.getDefauted(), record.getRevision()));
  }

  /**
   * 单条数据查询
   *
   * @param id 主键ID|1
   * @author lslands
   * @date 2024/5/19 23:05
   * @return SysDict
   */
  @GetMapping("/getById")
  public R<SysDictItem> getById(@RequestParam Long id) {
    return R.success(dictItemService.getById(id));
  }

  /**
   * 分页模糊查询列表
   * @param search 查询对象
   * @return  R<Page<SysDictItem>>
   * @throws BaseException 查询异常
   */
  @GetMapping("/page")
  public R<Page<SysDictItem>> page(@ModelAttribute DictItemSearch search) throws BaseException {
    BaseQuery query = dictItemConverter.queryEntity(search);
    return R.success(dictItemService.getPage(query, search.getDictCode()));
  }

  /**
   * 单条逻辑删除
   * @apiNote 该删除存在关联操作,删除字典目录，同时也会删除对应的词条
   * @param id 主键ID|1
   * @author lslands
   * @date 2024/5/19 22:28
   */
  @DeleteMapping("/deleteOne")
  public R deleteOne(@RequestBody Long id) throws BaseException {
    return toDeleteAjax(dictItemService.deleteOne(id));
  }

  /**
   * 批量删除
   * @apiNote 该删除存在关联操作,删除字典目录，同时也会删除对应的词条
   * @param ids 主键ID集|[1,2]
   * @param delete 是否物理删除标记|false
   * @author lslands
   * @date 2024/5/19 22:29
   */
  @DeleteMapping("/deleteBatch/{delete}")
  public R deleteBatch(@RequestBody List<Long> ids,@PathVariable("delete") Boolean delete) throws BaseException {
    if (ids.isEmpty()) {
      return toDeleteAjax(false);
    }
    return toDeleteAjax(dictItemService.deleteBatch(delete, ids));
  }
}
